<?php
namespace app\service;
use think\Db;
use think\facade\Log;
use printing\printDemo;
class swooleService{
    //月卡上传
    public function carPackage($indata){
        $parkinglotinfo=Db::table("parkinglotinfo")->where("parkingNo",$indata["parkingNo"])->find();
        if($indata["operate_type"]=="3"){
            //
            $res=Db::table("car_package")->where("parkingNo",$indata["parkingNo"])->where("car_number",$indata["car_number"])->delete();
            if($res){

            }else{

            }
        }else{
            //查询车辆是否存在
            $carinfo=Db::table("car_package")->where("parkingNo",$indata["parkingNo"])->where("car_number",$indata["car_number"])->find();
            $info["begin_time"]=$indata["begin_time"];
            $info["end_time"]=$indata["end_time"];
            $info["owner_name"]=$indata["name"];
            $info["owner_phone"]=$indata["tel"];
            $info["address"]=$indata["address"];
            $info["price"]=$indata["price"];
            $info["delivery_status"]=1;
            if(empty($carinfo)){
                $info["car_number"]=$indata["car_number"];
                $info["parkingNo"]=$indata["parkingNo"];
                $info["parkingName"]=$parkinglotinfo["parkingName"];
                Db::table("car_package")->insert($info);
            }else{
                Db::table("car_package")->where("id",$carinfo["id"])->update($info);
            }
        }

        $response["bizCode"]="B1034";
        $response["respCode"]="00000";
        $response["respMsg"]="成功";

        $parkingKey=$parkinglotinfo["parkingKey"];
        $response["sign"]=$this->MakeSign($response,$parkingKey);
        $result=$this->jsondata($response);
        return $result;
    }
    public function uploadIn($indata){
        $parkingOrderNo=date("YmdHis")."-".rand(10000000,99999999);
        $carin["parkingNo"]=$indata["parkingNo"];
        $carin["parkingName"]=$indata["parkingName"];
        $carin["reqNo"]=$indata["reqNo"];
        $carin["carPlateNo"]=$indata["carPlateNo"];
        $carin["clientNo"]=$indata["clientNo"];
        $carin["parkingStartTime"]=$indata["parkingStartTime"];
        //$carin["carColor"]=$indata["carColor"];
        $carin["parkingOrderNo"]=$parkingOrderNo;
        $carin["inChannelNo"]=$indata["inChannelNo"];
        $carin["inChannelName"]=$indata["inChannelName"];
        $carin["outParkingOrderNo"]=$indata["outParkingOrderNo"];
        
        $res=Db::table("car_order_in")->insertGetId($carin);
        
        $logininfo=Db::table("client_login")
                ->alias("a")
                ->join("parkinglotinfo b","a.parkingNo=b.parkingNo")
                ->where("a.clientNo",$indata["clientNo"])
                ->where("a.parkingNo",$indata["parkingNo"])
                ->find();
        //城市大脑
        if($logininfo["hzcity_status"]=="1"){
            $hzcity["parkingCode"]=$logininfo["parking_id"];
            $hzcity["uid"]=$res;
            $hzcity["arriveID"]=$res;
            $hzcity["gateNo"]=$logininfo["clientNo"];
            $hzcity["entryNum"]=$indata["inChannelNo"];
            $hzcity["car_number"]=$indata["carPlateNo"];
            $hzcity["arriveTime"]=date("Y-m-d H:i:s");
            $hzres=(new hzcityService())->uploadCarInData($hzcity);
            $respic="";
            if(isset($indata["content"])){
                $hzcity["type"]=1;
                $hzcity["content"]=$indata["content"];
                $respic=(new hzcityService())->uploadPhoto($hzcity);
                Log::write($hzcity, 'hzcity', true);
                //Log::write($respic, 'respic', true);
            }
            Db::table("car_order_in")->where("id",$res)->update(["upload_hzcity_status"=>"1","return_msg"=>$hzres,"return_msg_pic"=>$respic]);
        } 
        //支付宝无感
        /*if($logininfo["wg_status"]=="1"){
            $api=new apiparkService();
            $result = $api->enterinfo(json_encode(['parking_id' => $logininfo["parking_id"], 'car_number' => $indata["carPlateNo"], 'in_time' => date('Y-m-d H:i:s')]));
            //判断是否上传成功
        }*/
        
        
        $response["reqNo"]=$logininfo["reqNo"];
        $parkingKey=$logininfo["parkingKey"];
        //查询车场的parkingKey和reqNo
        if($res){
            $response["bizCode"]="B1008";
            $response["respCode"]="00000";
            $response["respMsg"]="成功";
            
            $response["parkingOrderNo"]=$parkingOrderNo;
            $response["sign"]=$this->MakeSign($response,$parkingKey);
            $result=$this->jsondata($response);
            return $result;
        }else{
            $response["bizCode"]="B1008";
            $response["respCode"]="20000";
            $response["respMsg"]="其他错误";
            
            $response["sign"]=$this->MakeSign($response,$parkingKey);
            $result=$this->jsondata($response);
            return $result;
        }
    }

    public function carOut($indata){
        $info["parkingNo"]=$indata["parkingNo"];
        $info["parkingName"]=$indata["parkingName"];
        $info["clientNo"]=$indata["clientNo"];
        $info["outParkingOrderNo"]=$indata["parkingOrderNo"];
        $info["amountRec"]=$indata["amountRec"];
        $info["amountDue"]=$indata["amountDue"];
        $info["carPlateNo"]=$indata["carPlateNo"];
        $info["parkingStartTime"]=$indata["parkingStartTime"];
        $info["parkingEndTime"]=date("YmdHis");
        $info["user_id"]=$indata["outOpr"];
        $info["paytime"]=date("YmdHis");
        $info["reqNo"]=$indata["reqNo"];
        $info["parkingTime"]=$indata["parkingTime"];
        $info["outChannelNo"]=$indata["outChannelNo"];
        $info["payType"]=$indata["payType"];
        $res=1;
        //查询该车辆是否是月租车
        $packageInfo=Db::table("car_package")->where("parkingNo",$indata["parkingNo"])->where("car_number",$indata["carPlateNo"])->find();
        if(($info["payType"]=="cash")&&($info["amountRec"]!="0")&&empty($packageInfo)){
            //查询该订单是否重复
            $orderinfo=Db::table("car_order")->where("outParkingOrderNo", $info["outParkingOrderNo"])->where("parkingNo",$info["parkingNo"])->find();
            if(empty($orderinfo)){
                //现金收款打印
                $res=Db::table("car_order")->insert($info);
                $clientInfo=(new orderService())->clientInfo($info["parkingNo"],$info["clientNo"]);
                if(!empty($clientInfo["terminal_yly"])){
                    $pay_type="现金";
                    //(new printDemo())->carOrder($clientInfo["terminal_yly"],$info["parkingName"],$info["carPlateNo"],$info["parkingStartTime"],date("YmdHis"),ctime($info["parkingTime"]),$info["amountRec"]/100,$pay_type);
                }
            }else{
                $infos["parkingTime"]=$indata["parkingTime"];
                $infos["outChannelNo"]=$indata["outChannelNo"];
                $infos["parkingEndTime"]=$indata["parkingStartTime"];
                Db::table("car_order")->where("order_id",$orderinfo["order_id"])->update($infos);
            }

        }



        //查询客户端登录的信息
        $loginfo=Db::table("client_login")
            ->alias("a")
            ->join("parkinglotinfo b","a.parkingNo=b.parkingNo")
            ->where("a.clientNo",$indata["clientNo"])
            ->where("a.parkingNo",$indata["parkingNo"])
            ->find();
        if($loginfo["hzcity_status"]=="1"){
            //查询是否有入场信息
            $orderin=Db::table("car_order_in")->where("outParkingOrderNo",$indata["parkingOrderNo"])->find();

            if(!empty($orderin)){
                $hzcity["parkingCode"]=$loginfo["parking_id"];
                $hzcity["car_number"]=$indata["carPlateNo"];
                $hzcity["entryNum"]=$orderin["inChannelNo"];
                $hzcity["outNum"]=$indata["outChannelNo"];
                $hzcity["leftCounts"]=$indata["leftCounts"];
                $hzcity["totalBerthNum"]=$loginfo["totalBerthNum"];
                $hzcity["uid"]=$orderin["id"];
                $hzcity["gateNo"]=$loginfo["clientNo"];
                $hzcity["arriveTime"]=date("Y-m-d H:i:s",strtotime($orderin["parkingStartTime"]));

                $hzres=(new hzcityService())->uploadCarOutData($hzcity);
                $respic="";
                if(isset($indata["content"])){
                    $hzcity["arriveID"]=$orderin["id"];
                    $hzcity["type"]=2;
                    $hzcity["content"]=$indata["content"];
                    $respic=(new hzcityService())->uploadPhoto($hzcity);
                    Log::write($hzcity, 'hzcity', true);
                    //Log::write($respic, 'respic', true);
                }
                Db::table("car_order_in")->where("id",$orderin["id"])->update(["upload_hzcity_status_out"=>"1","return_msg_out"=>$hzres,"return_msg_pic_out"=>$respic]);
            }
        }
        if($res){
            $response["bizCode"]="B10012";
            $response["reqNo"]=$loginfo["reqNo"];
            $response["respCode"]="00000";
            $response["respMsg"]="成功";
            $parkingKey=$loginfo["parkingKey"];
            $response["sign"]=$this->MakeSign($response,$parkingKey);
            $result=$this->jsondata($response);

            return $result;
        }else{
            $response["bizCode"]="B10012";
            $response["reqNo"]=$loginfo["reqNo"];
            $response["respCode"]="10000错误";
            $response["respMsg"]="错误";
            $parkingKey=$loginfo["parkingKey"];
            $response["sign"]=$this->MakeSign($response,$parkingKey);
            $result=$this->jsondata($response);
            return $result;
        }
    }
    
    public function wuganCommand($indata,$loginfo){
        $order["parkingNo"]=$indata["parkingNo"];
        $order['carPlateNo'] = $indata["carPlateNo"];//车牌
        
        $order['out_trade_no'] =$indata["parkingOrderNo"];//支付宝合作商户网站唯一订单号
        $order['subject'] =$indata["parkingName"]."-".$indata["carPlateNo"].'代扣缴费';//订单标题
        $total_fee =$indata["amountRec"]/100;;//订单金额
        //根据停车场查询商户账号
        $parking_id=$loginfo["parking_id"];
        $propertyinfo = (new orderService())->propertyInfo($parking_id);
        $agent_id = 2088621722997814;//代扣返佣的支付宝用户号
        $body = '停车免密支付';//订单描述
        $api=new apiparkService();
        $orderpay = $api->orderPay(json_encode(['car_number' => $order['carPlateNo'], 'out_trade_no' => $order['out_trade_no'] , 'subject' => $order['subject'], 'total_fee' => $total_fee, 'seller_logon_id' => $propertyinfo['account_no'], 'seller_id' => $propertyinfo['mchnt_id'], 'parking_id' => $loginfo["parking_id_wg"], 'out_parking_id' => $order["parkingNo"], 'agent_id' => $agent_id, 'body' => $body]));
        //Log::write($orderpay, 'orderpay', true);
        if(property_exists($orderpay,'sub_msg')){
            
            $response["bizCode"]="B1010";
            $response["reqNo"]=$loginfo["reqNo"];
            $response["respCode"]="10000";
            $response["respMsg"]="其他错误";
            $parkingKey=$loginfo["parkingKey"];
            $response["sign"]=$this->MakeSign($response,$parkingKey);
            $result=$this->jsondata($response);
            return $result;
        }else{
            $response["bizCode"]="B1013";
            $response["parkingNo"]=$indata["parkingNo"];
            $response["parkingName"]=$indata["parkingName"];
            $response["clientNo"]=$indata["clientNo"];
            $response["reqNo"]=date("YmdHis")."-".rand(100000,999999);
            $response["version"]="V1.0";
            $response["parkingStartTime"]=$indata["parkingStartTime"];
            $response["carPlateNo"]=$indata["carPlateNo"];
            $response["outParkingOrderNo"]=$indata["parkingOrderNo"];
            $response["tradeNo"]=$orderpay->trade_no;
            $orderType="outPay";
            $response["orderType"]=$orderType;
            $response["outChannelNo"]=$indata["outChannelNo"];;
            $response["payStatus"]="SUCCESS";
            $response["payDateTime"]=date("YmdHis");;
            $response["amountDue"]=$indata["amountDue"];
            $response["amountRec"]=$indata["amountRec"];
            $parkingKey=$loginfo["parkingKey"];
            $response["sign"]=$this->MakeSign($response,$parkingKey);
            $result=$this->jsondata($response);
            
            
            $order["parkingName"]=$response["parkingName"];
            $order["clientNo"]=$response["clientNo"];
            $order["outParkingOrderNo"]=$response["outParkingOrderNo"];
            $order["amountDue"]=$response["amountDue"];
            $order["amountRec"]=$response["amountRec"];
            $order["parkingStartTime"]=$indata["parkingStartTime"];
            $order["paytime"]=$response["payDateTime"];
            $order["reqNo"]=$response["reqNo"];
            $order["parkingTime"]=$indata["parkingTime"];
            $order["carPlateNo"]=$indata["carPlateNo"];
            $order["outChannelNo"]=$response["outChannelNo"];
            $order["payType"]="5";
            $order["parkingEndTime"]=$response["payDateTime"];
            $order["tradeno"]=$orderpay->trade_no;
            $order["user_id"]=$orderpay->user_id;
            $uid=Db::table("car_order")->insertGetId($order);

            //打印
            return $result;
        }
    }
    public function hzcityCommand($indata,$loginfo){
        $response["bizCode"]="B1013";
        $response["parkingNo"]=$indata["parkingNo"];
        $response["parkingName"]=$indata["parkingName"];
        $response["clientNo"]=$indata["clientNo"];
        $response["reqNo"]=date("YmdHis")."-".rand(100000,999999);
        $response["version"]="V1.0";
        $response["parkingStartTime"]=$indata["parkingStartTime"];
        $response["carPlateNo"]=$indata["carPlateNo"];
        $response["outParkingOrderNo"]=$indata["parkingOrderNo"];
        $response["tradeNo"]=date("YmdHis")."-".rand(100000,999999);
        $orderType="outPay";
        $response["orderType"]=$orderType;
        $response["outChannelNo"]=$indata["outChannelNo"];;
        $response["payStatus"]="SUCCESS";
        $response["payDateTime"]=date("YmdHis");;
        $response["amountDue"]=$indata["amountDue"];
        $response["amountRec"]=$indata["amountRec"];
        $parkingKey=$loginfo["parkingKey"];
        $response["sign"]=$this->MakeSign($response,$parkingKey);
        $result=$this->jsondata($response);
        
        $order["parkingNo"]=$response["parkingNo"];
        $order["parkingName"]=$response["parkingName"];
        $order["clientNo"]=$response["clientNo"];
        $order["outParkingOrderNo"]=$response["outParkingOrderNo"];
        $order["amountDue"]=$response["amountDue"];
        $order["amountRec"]=$response["amountRec"];
        $order["parkingStartTime"]=$indata["parkingStartTime"];
        $order["paytime"]=$response["payDateTime"];
        $order["reqNo"]=$response["reqNo"];
        $order["parkingTime"]=$indata["parkingTime"];
        $order["carPlateNo"]=$indata["carPlateNo"];
        $order["outChannelNo"]=$response["outChannelNo"];
        $order["payType"]="4";
        $order["parkingEndTime"]=$response["payDateTime"];
        $order["tradeno"]=$response["tradeNo"];
        $order["orderstatus"]="WAIT";
        $order["billID"]=date("YmdHis").rand(100000,999999);
        $uid=Db::table("car_order")->insertGetId($order);
        if($uid){
            $hzcity["parkingCode"]=$loginfo["parking_id"];
            $hzcity["uid"]=$uid;
            $hzcity["plateNo"]=$indata["carPlateNo"];
            $hzcity["plateColor"]="1";
            $hzcity["arriveTime"]=date("Y-m-d H:i:s",strtotime($indata["parkingStartTime"]));
            $hzcity["endTime"]=date("Y-m-d H:i:s",strtotime($response["payDateTime"]));
            $hzcity["billID"]=$order["billID"];
            $hzcity["billTime"]=date("Y-m-d H:i:s");
            $hzcity["shouldPay"]=$indata["amountRec"];;
            $hzcity["payStatus"]="0";//0-未完成 1-已完成
            $hzcity["carStock"]="1";//0-车辆在场 1-车辆已离场
            $res=(new hzcityService())->applyPayment($hzcity);
        }
        
        return $result;
    }
    
    //获取福利车订单参数
    public function getCarWelfareOrder($indata,$carwelfareInfo){
        $car_welfare_order["in_year"]=date("Y",strtotime($indata["parkingStartTime"]));
		$car_welfare_order["in_month"]=date("m",strtotime($indata["parkingStartTime"]));
		$car_welfare_order["halfyear"]=1;
		if($car_welfare_order["in_month"]>6){
			$car_welfare_order["halfyear"]=2;
		}
		$car_welfare_order["car_number"]=$indata["carPlateNo"];
		
		$car_welfare_order["fee_duration"]=$indata["parkingTime"];
		$car_welfare_order["parkingNo"]=$indata["parkingNo"];
		$car_welfare_order["parkingName"]=$indata["parkingName"];
		$car_welfare_order["in_time"]=$indata["parkingStartTime"];
		$car_welfare_order["out_time"]=$indata["parkingEndTime"];
		$car_welfare_order["create_time"]=time();
		
		$car_welfare_order["room_id"]=$carwelfareInfo["room_id"];
		$car_welfare_order["address"]=$carwelfareInfo["address"];
					
		return $car_welfare_order;
    }
    //获取福利车当前已使用的时间
    public function getCarWelfareTime($carwelfareInfo,$car_welfare_order){
        $time=Db::table("car_welfare_order")
				->where("room_id",$carwelfareInfo["room_id"])
				->where("in_year",$car_welfare_order["in_year"])
				->where("halfyear",$car_welfare_order["halfyear"])
				->where("pstatus",1)
				->sum("fee_duration");
		return $time;
    }
    //福利车返回的命令
    public function carwelfareCommand($indata,$loginfo,$car_welfare_order){
        $response["bizCode"]="B1013";
        $response["parkingNo"]=$indata["parkingNo"];
        $response["parkingName"]=$indata["parkingName"];
        $response["clientNo"]=$indata["clientNo"];
        $response["reqNo"]=date("YmdHis")."-".rand(100000,999999);
        $response["version"]="V1.0";
        $response["parkingStartTime"]=$indata["parkingStartTime"];
        $response["carPlateNo"]=$indata["carPlateNo"];
        $response["outParkingOrderNo"]=$indata["parkingOrderNo"];
        
        $response["tradeNo"]=date("Y-m-d H:i:s")."-".rand(100000,999999);
        $orderType="outPay";
        $response["orderType"]=$orderType;
        $response["outChannelNo"]=$indata["outChannelNo"];;
        $response["payStatus"]="SUCCESS";
        $response["payDateTime"]=date("YmdHis");;
        $response["amountDue"]=$indata["amountDue"];
        $response["amountRec"]="0";
        $parkingKey=$loginfo["parkingKey"];
        $response["sign"]=$this->MakeSign($response,$parkingKey);
        $result=$this->jsondata($response);

        $order["parkingNo"]=$response["parkingNo"];
        $order["parkingName"]=$response["parkingName"];
        $order["clientNo"]=$response["clientNo"];
        $order["outParkingOrderNo"]=$response["outParkingOrderNo"];
        $order["amountDue"]=$response["amountDue"];
        $order["amountRec"]=$response["amountRec"];
        $order["parkingStartTime"]=$indata["parkingStartTime"];
        $order["paytime"]=$response["payDateTime"];
        $order["reqNo"]=$response["reqNo"];
        $order["parkingTime"]=$indata["parkingTime"];
        $order["carPlateNo"]=$indata["carPlateNo"];
        $order["outChannelNo"]=$response["outChannelNo"];
        $order["payType"]="7";
        $order["parkingEndTime"]=$response["payDateTime"];
        $order["tradeno"]=$response["tradeNo"];
        Db::startTrans();
        try {
            $order_id=Db::table("car_order")->insertGetId($order);
            $car_welfare_order["order_id"]=$order_id;
            Db::table('car_welfare_order')->insert($car_welfare_order);
            // 提交事务
            Db::commit();
            return $result;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return ;
        }
        
        
    }
    
    
    //储值车返回的命令
    public function carvalueCommand($indata,$carvalueInfo,$loginfo){
        
        $response["bizCode"]="B1013";
        $response["parkingNo"]=$indata["parkingNo"];
        $response["parkingName"]=$indata["parkingName"];
        $response["clientNo"]=$indata["clientNo"];
        $response["reqNo"]=date("YmdHis")."-".rand(100000,999999);
        $response["version"]="V1.0";
        $response["parkingStartTime"]=$indata["parkingStartTime"];
        $response["carPlateNo"]=$indata["carPlateNo"];
        $response["outParkingOrderNo"]=$indata["parkingOrderNo"];
        //$response["outParkingOrderNo"]=$indata["outParkingOrderNo"];
        $response["tradeNo"]=date("Y-m-d H:i:s")."-".rand(100000,999999);
        $orderType="outPay";
        $response["orderType"]=$orderType;
        $response["outChannelNo"]=$indata["outChannelNo"];;
        $response["payStatus"]="SUCCESS";
        $response["payDateTime"]=date("YmdHis");;
        $response["amountDue"]=$carvalueInfo["price"]*100;
        $response["amountRec"]=$carvalueInfo["price"]*100;
        $parkingKey=$loginfo["parkingKey"];
        $response["sign"]=$this->MakeSign($response,$parkingKey);
        $result=$this->jsondata($response);

        $order["parkingNo"]=$response["parkingNo"];
        $order["parkingName"]=$response["parkingName"];
        $order["clientNo"]=$response["clientNo"];
        $order["outParkingOrderNo"]=$response["outParkingOrderNo"];
        $order["amountDue"]=$response["amountDue"];
        $order["amountRec"]=$response["amountRec"];
        $order["parkingStartTime"]=$indata["parkingStartTime"];
        $order["paytime"]=$response["payDateTime"];
        $order["reqNo"]=$response["reqNo"];
        $order["parkingTime"]=$indata["parkingTime"];
        $order["carPlateNo"]=$indata["carPlateNo"];
        $order["outChannelNo"]=$response["outChannelNo"];
        $order["payType"]="6";
        $order["parkingEndTime"]=$response["payDateTime"];
        $order["tradeno"]=$response["tradeNo"];
        Db::startTrans();
        try {
            Db::table("car_order")->insert($order);
            Db::table('car_value')->where('id', $carvalueInfo["id"])->setDec('balance',$carvalueInfo["price"]);
            // 提交事务
            Db::commit();
            return $result;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }
    }
    //优惠券支付返回的命令
    public function couponCommand($indata,$loginfo){
        $response["bizCode"]="B1013";
        $response["parkingNo"]=$indata["parkingNo"];
        $response["parkingName"]=$indata["parkingName"];
        $response["clientNo"]=$indata["clientNo"];
        $response["reqNo"]=date("YmdHis")."-".rand(100000,999999);
        $response["version"]="V1.0";
        $response["parkingStartTime"]=$indata["parkingStartTime"];
        $response["carPlateNo"]=$indata["carPlateNo"];
        $response["outParkingOrderNo"]=$indata["parkingOrderNo"];
        //$response["outParkingOrderNo"]=$indata["outParkingOrderNo"];
        $response["tradeNo"]=date("Y-m-d H:i:s")."-".rand(100000,999999);
        $orderType="outPay";
        $response["orderType"]=$orderType;
        $response["outChannelNo"]=$indata["outChannelNo"];;
        $response["payStatus"]="SUCCESS";
        $response["payDateTime"]=date("YmdHis");;
        $response["amountDue"]=$indata["amountDue"];
        $response["amountRec"]="0";
        $parkingKey=$loginfo["parkingKey"];
        $response["sign"]=$this->MakeSign($response,$parkingKey);
        $result=$this->jsondata($response);

        $order["parkingNo"]=$response["parkingNo"];
        $order["parkingName"]=$response["parkingName"];
        $order["clientNo"]=$response["clientNo"];
        $order["outParkingOrderNo"]=$response["outParkingOrderNo"];
        $order["amountDue"]=$response["amountDue"];
        $order["amountRec"]=$response["amountRec"];
        $order["parkingStartTime"]=$indata["parkingStartTime"];
        $order["paytime"]=$response["payDateTime"];
        $order["reqNo"]=$response["reqNo"];
        $order["parkingTime"]=$indata["parkingTime"];
        $order["carPlateNo"]=$indata["carPlateNo"];
        $order["outChannelNo"]=$response["outChannelNo"];
        $order["payType"]="3";
        $order["parkingEndTime"]=$response["payDateTime"];
        $order["tradeno"]=$response["tradeNo"];
        
        Db::table("car_order")->insert($order);
        return $result;
    }
    
    
    //根据车场和客户端编号查询登录的fd
    public function loginfo($parkingNo,$clientNo){
        return Db::table("client_login")
                ->alias("a")
                ->join("parkinglotinfo b","a.parkingNo=b.parkingNo")
                ->where("a.clientNo",$clientNo)
                ->where("a.parkingNo",$parkingNo)
                ->field("a.*,b.parkingKey,b.hzcity_status,b.wg_status,b.parking_id,b.parking_id_wg,b.welfare_time")
                ->find();
    }
    //查询该车辆是否是优惠车辆
    public function couponCarInfo($parkingNo,$carPlateNo){
        //
        //更新过期的优惠车辆为无效状态
        Db::table("car_coupon")->where("end_time","<",time())->update(["coupon_status"=>"2"]);
        $couponCarInfo=Db::table("car_coupon")->where("parkingNo",$parkingNo)->where("carPlateNo",$carPlateNo)->where("coupon_status","1")->find();
        return $couponCarInfo;
    }
    
     //查询车辆是否是福利车
    public function carwelfareInfo($parkingNo,$carPlateNo){
        $couponCarInfo=Db::table("car_welfare_owner")->where("parkingNo",$parkingNo)->where("car_number",$carPlateNo)->find();
        return $couponCarInfo;
    }
    //查询车辆是否是储值车
    public function carvalueInfo($parkingNo,$carPlateNo){
        $couponCarInfo=Db::table("car_value")->where("parkingNo",$parkingNo)->where("car_number",$carPlateNo)->find();
        return $couponCarInfo;
    }
    //上传入场信息
    
    //登录
    public function login($indata,$fd){
        $res=Db::table("parking_client")
                    ->alias("a")
                    ->join("parkinglotinfo b","a.parkingNo=b.parkingNo")
                    ->where("a.clientNo",$indata["clientNo"])
                    ->where("a.parkingNo",$indata["parkingNo"])
                    ->find();
                    
        if(empty($res)){
            $parkingKey=$res["parkingKey"];
            $response["bizCode"]="B1002";
            $response["respCode"]="10001";
            $response["respMsg"]="停车场不存在";
            $response["reqNo"]=$indata["reqNo"];
            $response["sign"]=$this->MakeSign($response,$parkingKey);
            $result=$this->jsondata($response);
            return $result;
        }
        $parkingKey=$res["parkingKey"];
        
        $data=Db::table("client_login")->where("clientNo",$indata["clientNo"])->where("parkingNo",$indata["parkingNo"])->find();
        //return $data;
        if(!empty($data)){
            $sql2="update client_login set connect_status='ON' ,fd='".$fd."',reqNo='".$indata["reqNo"]."' where clientNo='".$indata["clientNo"]."' and parkingNo='".$indata["parkingNo"]."'";
            $res2 =Db::execute($sql2);
            if($res2){
                $response["bizCode"]="B1002";
                $response["respCode"]="00000";
                $response["respMsg"]="成功";
                $response["reqNo"]=$indata["reqNo"];
                $response["sign"]=$this->MakeSign($response,$parkingKey);
                $result=$this->jsondata($response);
                return $result;
            }else{
                $response["bizCode"]="B1002";
                $response["respCode"]="10000";
                $response["respMsg"]="登录失败";
                $response["reqNo"]=$indata["reqNo"];
                $response["sign"]=$this->MakeSign($response,$parkingKey);
                $result=$this->jsondata($response);
                return $result;
            }
        }else{
            $sql2="insert into client_login(fd,clientNo,connect_status,reqNo,parkingNo) values('".$fd."','".$indata["clientNo"]."','ON','".$indata["reqNo"]."','".$indata["parkingNo"]."')";
            $res2 =Db::execute($sql2);
            if($res2){
                $response["bizCode"]="B1002";
                $response["respCode"]="00000";
                $response["respMsg"]="成功";
                $response["reqNo"]=$indata["reqNo"];
                $response["sign"]=$this->MakeSign($response,$parkingKey);
                $result=$this->jsondata($response);
                return $result;
            }else{
                $response["bizCode"]="B1002";
                $response["respCode"]="10000";
                $response["respMsg"]="登录失败";
                $response["reqNo"]=$indata["reqNo"];
                $response["sign"]=$this->MakeSign($response,$parkingKey);
                $result=$this->jsondata($response);
                return $result;
            }
        }
    }
    
    
    //心跳
    public function heartbeat($indata){
        $response["bizCode"]="B1006";
        $response["respCode"]="00000";
        $response["respMsg"]="成功";
        
        
        $res=Db::table("client_login")
                ->alias("a")
                ->join("parkinglotinfo b","a.parkingNo=b.parkingNo")
                ->where("a.clientNo",$indata["clientNo"])
                ->where("a.parkingNo",$indata["parkingNo"])
                ->find();
        $response["reqNo"]=$res["reqNo"];
        $parkingKey=$res["parkingKey"];
        //从redis中读取reqNo,parkingNo
        $response["sign"]=$this->MakeSign($response,$parkingKey);
        $result=$this->jsondata($response);
        return $result;
    }
    private function jsondata($arr){
        $str='{';
        foreach($arr as $key =>$value){
            $str=$str.'"'.$key.'":"'.$value.'",';
        }
        $str = trim($str, ",");
        $str=$str.'}';
        return $str;
    }
    private function MakeSign($arr,$parkingKey){
        //签名步骤一：按字典序排序参数
        ksort($arr);
        $string = $this->ToUrlParams($arr);
        //return $string;
        //签名步骤二：在string后加入KEY
        $string = $string . "&key=" . $parkingKey;
        //签名步骤三：MD5加密
        $string = md5($string);
        //签名步骤四：所有字符转为大写
        $result = strtoupper($string);
        return $result;
    }
    
    //格式化参数格式化成url参数(str)
    private function ToUrlParams($value){
        $buff = "";
        foreach($value as $k => $v){
            if($k != "sign" && $v != "" && !is_array($v)){
                $buff .= strtolower($k) . "=" . $v . "&";
            }
        }
        $buff = trim($buff, "&");
        return $buff;
    }
}